package com.wsc.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.wsc.service.ConsumeLogService;
import com.wsc.service.StudentService;
import com.wsc.service.SwipingCardService;
import com.wsc.service.TeacherService;

@RequestMapping("/card")
@RestController
@CrossOrigin
@EnableScheduling // 定时执行
public class SwipingCardController {

	@Autowired
	private SwipingCardService ss;

	@Autowired
	private StudentService studentService;

	@Autowired
	private TeacherService teacherService;

	@Autowired
	private ConsumeLogService cs;

	// 刷卡
	@RequestMapping("/swipingCard")
	public Map<String, Object> swipingCard(String cardno, Integer readroomid) {
		// map存储信息
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("cardno", cardno);
		map.put("readroomid", readroomid);
		// String、Integer默认值为null 参数为有效数据
		if (cardno != null || !("").equals(cardno) || readroomid != null) {
			// Student或者Teacher根据cardno是否查询出数据
			if (studentService.findByCardno(cardno) != null || teacherService.findByCardno(cardno) != null) {
				return ss.swipingCard(map);
			}
		}
		map.put("code", 0);
		map.put("msg", "卡号输入异常，请联系阅览室负责人！");
		return map;
	}

	// 晚上10点执行修改未刷卡教师或者学生的状态ConsumeLog状态
	@Scheduled(cron = "0 * 22 * * ?")
	public void updateStaus() {
		cs.updateAllStatusToZero();
		studentService.updateAllStatusToZero();
		teacherService.updateAllStatusToZero();
		System.out.println("执行一次定时任务");
	}

}
